package com.example.synatx.parallel;

import org.junit.Test;

import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**http://172.20.48.26:10000/#/dashboard
 * spliterator是Java8新加入的接口.代表 可分迭代器.和iterator一样,spliterator也用于遍历数据源中的元素,但它是为了并行执行而设计的.
 * Java8已经为集合框架中包含的所哟偶数据结构提供了一个默认的Spliterator实现.
 *
 */
public class SpliteratorTest {

    final String str = "abc   mezzo del cammin  di nostra  vita "+ //3+1+1+2+1+2+1 = 11
            "mi  ritrovai in una  selva oscura"+ //2+1+1+2+1 = 7
            " che la  dritta via era   smarrita"; //1+1+2+1+1+3 = 9

    /**
     * 统计单词数
     */
    @Test
    public void counterWhiteSpace(){
        int counter = 0;
        boolean lastspace = true;
        for ( char c : str.toCharArray()){
            if(Character.isWhitespace(c)){
                lastspace = true;
               // counter++;
            }else{
                if(lastspace) counter++;
                lastspace = false;
            }
        }
        System.out.println(counter);
    }

    @Test
    public void test02(){
        Stream<Character> stream = IntStream.range(0,str.length()).mapToObj(str::charAt);
        WordCounter wordCounter = stream.reduce(new WordCounter(0,true),WordCounter::accumulate,WordCounter::combine);
        System.out.println(wordCounter.getCounter());
    }

    class WordCounter{
        private final int counter;
        private final boolean lastSpace;

        public WordCounter(int counter,boolean lastSpace){
            this.counter = counter;
            this.lastSpace = lastSpace;
        }

        public WordCounter accumulate(Character c){
            //上一个字符是空格,而当前遍历的字符串不是空格时,将单词计算器加1.
            if(Character.isWhitespace(c)){
                return lastSpace?this:new WordCounter(counter,true);
            }else{
                return lastSpace?new WordCounter(counter+1,false):this;
            }
        }

        public WordCounter combine(WordCounter wordCounter){
            return new WordCounter(counter+ wordCounter.counter,wordCounter.lastSpace);
        }

        public int getCounter(){
            return this.counter;
        }
    }
    class WordCountSpliterator implements Spliterator<Character>{
        private final String string;
        private int currentChar = 0;

        public WordCountSpliterator(String string){
            this.string = string;
        }

        @Override
        public boolean tryAdvance(Consumer<? super Character> consumer) {
            consumer.accept(string.charAt(currentChar++));
            return currentChar<string.length();//如果还有字符要处理,就返回true
        }

        @Override
        public Spliterator<Character> trySplit() {
            return null;
        }

        @Override
        public long estimateSize() {
            return string.length()-currentChar;
        }

        @Override
        public int characteristics() {
            return ORDERED+SIZED+SUBSIZED+NONNULL+IMMUTABLE;
        }
    }
}
